<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/12/8
 * Time: 09:32
 */

namespace app\api\validate;


use app\lib\exception\ParameterException;

class CardProductParameter extends BaseValidate
{

    protected $rule=[
      'goods'=>'checkProducts'
    ];

    protected $singleRule=[
        'goods_id'=>'isPositiveInt',
    ];
    /**
     * 验证数组
     * @param $values
     * @return bool
     * @throws ParameterException
     */
    public function checkProducts($values){
        if (empty($values)){
           return true;
        }
        if (!is_array($values)){
            throw new ParameterException([
                'msg'=>'商品参数不正确'
            ]);
        }
        foreach ($values as $value){
            $this->checkProduct($value);
        }
        return true;
    }
    /**
     * 验证数组里面的每个元素
     * @param $product
     * @return bool
     * @throws ParameterException
     */
    public function checkProduct($product){
        $validate  = new BaseValidate($this->singleRule);
        $result = $validate->check($product);
        if (!$result){
            throw new ParameterException([
                'msg'=>'商品参数不正确'
            ]);
        }
        return true;
    }

}